<template>
  <div class="app-page">
    <el-table-form ref="form" :model="query">
      <el-form-item label="功能属性:" prop="keyFun">
        <el-select v-model="query.keyFun" placeholder="请选择" clearable>
          <el-option
            v-for="item in keyFunMap"
            :key="item.value"
            :label="item.desc"
            :value="item.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="状态:" prop="keyStatus">
        <el-select v-model="query.keyStatus" placeholder="请选择" clearable>
          <el-option :value="1" label="未启用" />
          <el-option :value="2" label="启用" />
        </el-select>
      </el-form-item>
      <template v-slot:btns>
        <el-button type="primary" icon="el-icon-search" @click="search">查询</el-button>
      </template>
    </el-table-form>
    <div style="margin-bottom: 10px;">
      <el-button type="primary" icon="el-icon-plus" @click="add">新增字典</el-button>
    </div>
    <lb-table
      ref="lbTable"
      v-loading="$loadingPlugin.fetchData"
      v-el-height-adaptive-table="{bottomOffset:10}"
      border
      stripe
      element-loading-text="加载中"
      :column="tableData.column"
      :data="tableData.data"
      highlight-current-row
      pagination
      :total="tableData.total"
      custom-column
      @fetch="fetchData"
    />
    <add-dic ref="addDic" :key-fun-map="keyFunMap" />
  </div>
</template>

<script>
  import { dicDelete, dicQueryList, queryKeyFunMap } from '@/api'
  import AddDic from './components/AddCustomize'
  import { formatColumnMap } from '@/utils'

  export default {
    name: 'customize-tab',
    components: {
      AddDic
    },
    data() {
      return {
        keyFunMap: [],
        query: {
          keyFun: '',
          keyStatus: ''
        },
        tableData: {
          total: 0,
          column: [
            {
              type: 'index',
              align: 'center'
            },
            {
              prop: 'keyFun',
              label: '功能属性',
              render: (h, { row }) => <span>{ row.keyFun && formatColumnMap(this.keyFunMap, 'value', row.keyFun, 'desc') }</span>
            },
            {
              prop: 'keyAttr',
              label: '属性类别',
              render: (h, { row }) => {
                const item = this.keyFunMap.find(item => item.value === row.keyFun)
                let keyAttrs = []
                if (item) keyAttrs = item.tag
                if (keyAttrs.length === 0) return ''
                const arr = keyAttrs.split(',')
                const match = arr.find(item => Number(item.split(':')[0]) === Number(row.keyAttr))
                if (match) return <span>{ match.split(':')[1] }</span>
              }
            },
            {
              prop: 'keyName',
              label: '字典名称'
            },
            {
              prop: 'keyStatus',
              label: '状态',
              render: (h, { row }) => {
                if (row.keyStatus === 2) {
                  return <span style='color: #00bfff;'>启用</span>
                } else if (row.keyStatus === 1) {
                  return <span style='color: #F24024;'>未启用</span>
                } else {
                  return ''
                }
              }
            },
            {
              prop: 'remark',
              label: '备注'
            },
            {
              label: '操作',
              align: 'center',
              fixed: 'right',
              width: 90,
              render: (h, { row, $index }) => {
                return (
                  <div>
                    <el-link type='primary' size='mini' onClick={() => this.update(row, $index)}>
                      编辑
                    </el-link>
                    <el-link size='mini' type='danger' onClick={() => this.del(row, $index)}>
                      删除
                    </el-link>
                  </div>
                )
              }
            }
          ],
          data: []
        }
      }
    },
    created() {
      this.queryOptionsMap()
    },
    methods: {
      _getQuery(obj) {
        const query = {}
        for (const key in obj) {
          if (obj.hasOwnProperty(key)) {
            if (obj[key] !== '') {
              query[key] = obj[key]
            }
          }
        }
        return query
      },
      async fetchData({ currentPage, pageSize }) {
        const query = {
          ...this._getQuery(this.query),
          currentPage,
          pageSize
        }
        const res = await dicQueryList(query)
        this.tableData.data = res.data.rows.filter(item => item.keyFun !== 0)
        if (currentPage === 1) this.tableData.total = res.data.rows.filter(item => item.keyFun !== 0).length
      },
      async add() {
        await this.$refs.addDic.open()
        this.$refs.lbTable.fetch({ currentPage: 1 })
      },
      async update(row) {
        await this.$refs.addDic.open(row)
        this.$refs.lbTable.fetch({ currentPage: 1 })
      },
      search() {
        this.$refs.lbTable.fetch({ currentPage: 1 })
      },
      del(row, index) {
        this.$confirm('确定删除此字典项？', '提示', {
          type: 'warning'
        }).then(() => {
          const loading = this.$message({
            message: '正在删除',
            duration: 0,
            iconClass: 'el-icon-loading'
          })
          dicDelete({ keyId: row.keyId }).then(() => {
            this.tableData.data.splice(index, 1)
            this.$notify({
              title: '提示',
              message: '删除成功',
              type: 'success',
              duration: 2000
            })
          }).finally(() => {
            loading.close()
          })
        })
      },
      // 查询枚举
      async queryOptionsMap() {
        const res = await queryKeyFunMap()
        this.keyFunMap = res.data // 功能属性
      }
    }
  }
</script>

<style scoped>

</style>
